package com.tangtang.core.tree;

import com.tangtang.core.basic.Action;

public class TreeVisitor {
    /**
     * 遍历树节点
     *
     * @param <T>
     * @param node
     * @param action
     */
    public static <T> void visit(Tree<T> node, Action<Tree<T>> action) {
        if (node == null)
            return;
        action.apply(node);//visit root
        visitTree(node, action); //visit children
    }

    /**
     * @param node
     * @param action
     */
    private static <T> void visitTree(Tree<T> node, Action<Tree<T>> action) {
        if (node == null)
            return;
        if (node.getChildren() != null) {
            for (Tree<T> clilden : node.getChildren()) {
                action.apply(clilden);
                visitTree(clilden, action);
            }
        }
    }
}
